<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung - cuongnc.it659@gmail.com - Nguyễn Chí Cường.
    *
    */

	class model_product extends my_model
	{

		protected $_table;

		public function __construct()
        {
            parent::__construct();
            $this->_table = "tbl_product";
        }

		public function getHotproduct()
		{
			$this->where(array('product_hightlight'=>1,
								'product_status'=>1,
			));

			$this->get($this->_table);

			if($this->num_rows() > 0)
				return $this->fetch_all();
			return FALSE;
		}

		public function getProduct()
		{
			$this->where(array('product_status'=>1));

			$this->order('product_id','desc');

			$this->get($this->_table);

			if($this->num_rows() > 0)
				return $this->fetch_all();
			return FALSE;
		}

		public function search($search = array())
		{
			$keywords = $search['keywords'];
			$menu = $search['menu'];

			$keyword_array = explode(" ", $keywords);

			if($menu == '')
            {
                if(count($keyword_array) == 1)
                {
                	$strSQL = "SELECT * FROM $this->_table WHERE product_name LIKE '%$keyword_array[0]%'";

                    $this->query($strSQL);
                }
                else
                {
                    $strSQL = "SELECT * FROM $this->_table WHERE ";

                    $count = 0;

                    foreach($keyword_array as $keyword):
                        if($count == 0):
                            $strSQL .= " product_name LIKE '%$keyword%'";
                        else:
                            $strSQL .= " OR product_name LIKE '%$keyword%'";
                        endif;
                        $count ++;
                    endforeach;

                   	$this->query($strSQL);
                }
            }
			else
			{
				if(count($keyword_array) == 1)
                {
                    $strSQL = "SELECT * FROM $this->_table WHERE (menu_slug = '$menu')  AND (product_name LIKE '%$keyword_array[0]%')";

					$this->query($strSQL);
                }
                else
                {
                    $count = 0;

                    $strSQL = "SELECT * FROM $this->_table WHERE (menu_slug = '$menu') AND (";

                    foreach($keyword_array as $keyword):
                        if($count == 0):
                            $strSQL .= " product_name LIKE '%$keyword%'";
                        else:
                            $strSQL .= " OR product_name LIKE '%$keyword%'";
                        endif;

                    endforeach;

                    $strSQL .= ")";

                    $this->query($strSQL);
                }
			}
			if($this->num_rows() > 0)
				return $this->fetch_all();
			return FALSE;
		}

        public function getAllProducts($childs)
        {
            $strSQL = "SELECT * FROM tbl_product WHERE (";

            $count = 0;

            foreach($childs as $child_id):
                if($count == 0):
                    $strSQL .= " product_categories = $child_id ";
                else:
                    $strSQL .= " OR product_categories = $child_id ";
                endif;

                $count ++;
            endforeach;

            $strSQL .= " ) AND product_status = 1 ";

            //print($strSQL);
            $this->query($strSQL);

            if($this->num_rows() > 0)
                return $this->fetch_all();
            return FALSE;
        }

        public function getProductBySlug($data = array())
        {
            $data['product_status'] = 1;

            /*echo "<pre>";print_r($data);*/

            $this->where($data);

            $this->get($this->_table);

            if($this->num_rows() == 1)
                return $this->fetch_one();
            elseif($this->num_rows() > 1)
                return $this->fetch_all();
            return FALSE;

        }

        public function updateCounter($product_id)
        {
            $strSQL = "UPDATE ".$this->_table." SET product_counter = product_counter + 1 WHERE product_id = ".$product_id;

            $this->query($strSQL);
        }

        public function getRandomProduct($product_id)
        {
            $strSQL = "SELECT * FROM ".$this->_table." WHERE product_id <> ".$product_id." ORDER BY RAND() LIMIT 0,4";
            //print($strSQL);
            $this->query($strSQL);

            return $this->fetch_all();
        }
        
        public function getProductById($data = array())
        {
            $data['product_status'] = 1;
            
            $this->where($data);
            
            $this->get($this->_table);
            
            if($this->num_rows() > 0)
                return $this->fetch_one();
            return FALSE;
        }
        
        
        

	}


    /*End of layout.php*/
    /*Location: my_framework/controllers/layout.php */